 /*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.loginRegister;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.IOException;
import java.sql.*;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import javax.swing.JOptionPane;


/**
 *
 * @author Administrator
 */
public class LoginServlet extends HttpServlet {

    
     public void wrong1(){    //对话框提示信息
        String msg="用户名不能为空，登录失败！";
        int type=JOptionPane.YES_NO_CANCEL_OPTION;
        String title="提示信息";
        JOptionPane.showMessageDialog(null,msg,title,type);
    }
    public void wrong2(){    //对话框提示信息
        String msg="用户密码不能为空，登录失败！";
        int type=JOptionPane.YES_NO_CANCEL_OPTION;
        String title="提示信息";
        JOptionPane.showMessageDialog(null,msg,title,type);
    }
    public void wrong3(){    //对话框提示信息
        String msg="该用户尚未注册，登录失败！";
        int type=JOptionPane.YES_NO_CANCEL_OPTION;
        String title="提示信息";
        JOptionPane.showMessageDialog(null,msg,title,type);
    }
    public void wrong4(){    //对话框提示信息
        String msg="用户密码不正确，登录失败！";
        int type=JOptionPane.YES_NO_CANCEL_OPTION;
        String title="提示信息";
        JOptionPane.showMessageDialog(null,msg,title,type);
    }
    
    protected void doGet (HttpServletRequest request,HttpServletResponse response)
            throws ServletException,IOException{
        String userName = new
        String(request.getParameter("username").getBytes("ISO-8859-1"),"UTF-8");
        String password=new
        String(request.getParameter("password").getBytes("ISO-8859-1"),"UTF-8");
        if(userName.equals("")){
            wrong1();
            response.sendRedirect("http://localhost:8084/jspsxxxx/login/login/denglu.html");
        }
        else if(password.equals("")){
            wrong2();
            response.sendRedirect("http://localhost:8084/jspsxxxx/login/login/denglu.html");
        }
        else{
            try{
                Connection con=null;
                Statement stmt=null;
                ResultSet rs=null;
                Class.forName("com.mysql.jdbc.Driver");
                String url="jdbc:mysql://localhost:3306/abin? useUnicode=true&characterEncoding=gbk";
                con=DriverManager.getConnection(url,"root","admin");
                stmt=con.createStatement();
                String sql="select * from t_user where usernumber='"+userName+"'";
                rs=stmt.executeQuery(sql);
                int N=0;
                int P=0;
                while(rs.next()){
                    if(userName.equals(rs.getString("usernumber"))){
                        N=100;
                        if(password.equals(rs.getString("password"))){
                            P=100;
                            //实例化保存个人信息的JavaBean
                            LoginBean nn=new LoginBean();
                            nn.setUserName(userName);
                            nn.setPassword(password);
//                            nn.setname(name);
                            //获取session 对象
                            HttpSession session=request.getSession();
                            ArrayList login =new ArrayList();  //实例化列表对象
                            login.add(nn);                     //把个人信息保存到列表中
                            //把列表保存到session对象中，以便在别的页面中获取个人信息
                            session.setAttribute("login", login);
                            response.sendRedirect("http://localhost:8084/jspsxxxx/main/main/index.html");
                        }
                        else{
                            
                        }
                    }else{
                        N++;
                    }
                }
                if(N<100){
                    wrong3();
                    response.sendRedirect("http://localhost:8084/jspsxxxx/login/login/denglu.html");
                }
                else if(P<100){
                    wrong4();
                    response.sendRedirect("http://localhost:8084/jspsxxxx/login/login/denglu.html");
                }
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    }
    protected void doPost(HttpServletRequest request,HttpServletResponse response)
            throws ServletException,IOException{
        System.out.println("1");
        doGet(request,response);
    }
}
